﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Common;
using Service.DtoModel;
using Service.DtoModel.DeskApp;
using SqlSugar;

namespace Service.Implements
{
    public class SysTypeService : BaseServer<SysType>
    {
        /// <summary>
        /// 获取某个类型的最大id
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public int getMaxId(int group)
        {
            var res = 0;
            try
            {
                string _SQL = " select max(typeId) typeId from sys_type where typeGroup = @typeGroup";
                var maxId = Db.Ado.GetInt(_SQL, new List<SugarParameter>() { new SugarParameter("@typeGroup", group) });
                res = maxId;
            }
            catch (Exception ex)
            {
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 获取类型列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<SysTypeDto>>> getTypeList(SysTypeDto parm)
        {
            var res = new ApiResult<List<SysTypeDto>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                string _SQL = " select * from sys_type where typeGroup = @typeGroup";
                var _TB = Db.Ado.GetDataTable(_SQL, new List<SugarParameter>() { new SugarParameter("@typeGroup", parm.typeGroup) });
                List<SysTypeDto> _datas = new ModelHandler<SysTypeDto>().FillModel(_TB);
                res.data = _datas;
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> DeleteAsync(string parm)
        {
            var list = Utils.StrToListString(parm);
            var isok = await Db.Deleteable<SysType>().Where(m => list.Contains(m.id.ToString())).ExecuteCommandAsync();
            var res = new ApiResult<string>
            {
                success = isok > 0,
                statusCode = isok > 0 ? (int)ApiEnum.Status : (int)ApiEnum.Error,
                data = isok > 0 ? "1" : "0"
            };
            return res;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<SysType>>> GetPagesAsync(PageParm parm)
        {
            var res = new ApiResult<Page<SysType>>();
            try
            {
                res.data = await Db.Queryable<SysType>()
                                   .WhereIF(parm.typesId>0, m => m.typeGroup==parm.typesId)
                                   .WhereIF(!string.IsNullOrEmpty(parm.key), m => m.typeName.Contains(parm.key))
                                   .OrderBy(m => m.createDate, OrderByType.Desc)
                                   .ToPageAsync(parm.page, parm.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.statusCode = (int)ApiEnum.Error;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

  
    }
}
